<?

define('E', '../');
require_once 'ilib.php';

print "<pre>step 1\n";

if (is_file(E.'_lib/_dbconfig.php'))
	diags("Engine is already installed.");
else {

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		switch ($_POST['dbtype']) {
		
			case 1:	// mysql
				if ($check = check_db(array(
					'DBProto' => 'mysql', 
					'DBHost' => $_POST['dbhost1'], 
					'DBName' => $_POST['dbdb1'],
					'DBUser' => $_POST['dbuser1'],
					'DBPassword' => $_POST['dbpass1'],
					'DBPrefix' => $_POST['dbprefix1'],
					'DBCharset' => 'utf8')))
					diags($check);
				else {
					
//					header("Location: 2/");
				}
				break;
	
			case 2: // oracle
				if ($check = check_db(array(
					'DBProto' => 'oci8', 
					'DBHost' => $_POST['dbhost2'], 
					'DBName' => $_POST['dbdb2'],
					'DBUser' => $_POST['dbuser2'],
					'DBPassword' => $_POST['dbpass2'],
					'DBPrefix' => $_POST['dbprefix2'],
					'DBCharset' => 'utf8')))
					diags($check);
				else
					header("Location: 2/");

				break;
				
			default:
				diags('Unknown database server type');
				break;
		}
	} else 
		$DIAGS = array();

}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Installer. Step 1 of 2</title>
<link type="text/css" rel="stylesheet" href="/style/siteadmin.css" />
<script src="/lib/jquery.js"></script>
</head>
<body><h1>Site engine installer</h1>

<h2>Step 1 of 2: Setup database connection</h2>

<? if (sizeof($DIAGS)) { ?><p class="error"><?=implode('</p><p class="error">', $DIAGS)?></p><? } ?>

<form action="./" method="post">
<label><h3>Which database server are you going to use?</h3>
<select name="dbtype" id="db-choose">
<option value="0"></option>
<option value="1">MySQL</option>
<option value="2">Oracle</option>
</select></label>

<div class="req template" rel="1">
	<h3>Please fill the connection details</h3>
	<p><label>Server hostname<br>
		<input type="text" name="dbhost1"></label></p>
	<p><label>Database name<br>
		<input type="text" name="dbdb1"></label></p>
	<p><label>User name<br>
		<input type="text" name="dbuser1"></label></p>
	<p><label>Password<br>
		<input type="text" name="dbpass1"></label></p>
</div>

<div class="template" rel="1">
	<p><label>Tables' prefix<br>
		<input type="text" name="dbprefix1"></label></p>
</div>


<div class="req template" rel="2">
	<h3>Please fill the connection details</h3>
	<p><label>Server hostname<br>
		<input type="text" name="dbhost2"></label></p>
	<p><label>Oracle SID<br>
		<input type="text" name="dbdb2"></label></p>
	<p><label>User name<br>
		<input type="text" name="dbuser2"></label></p>
	<p><label>Password<br>
		<input type="text" name="dbpass2"></label></p>
</div>

<div class="template" rel="2">
	<p><label>Tables' prefix<br>
		<input type="text" name="dbprefix2"></label></p>
</div>

	<p><input type="submit" value="Save" class="template" disabled></p>
</form>

<script>jQuery(function($){

$("#db-choose").on("change", function(){
	var t = ~~$(this).val(), div;

	$(".req").hide();
	if (t && (div = $("[rel=" + t + "]")).length)	div.show();
	$("input[type=submit]").hide();
});

$(".req").on("change", "input", function(){
	var on = !($(this).closest(".req").find('input').map(function(i,e){ return $(e).val() ? undefined : 1 }).get().length);
console.log($(this).closest(".req").find('input').map(function(i,e){ return $(e).val() ? undefined : 1 }).get().length);	
	$("input[type=submit]").attr("disabled", !on);
	if (on) $("input[type=submit]").show();
});

});</script>
</body>
</html>